﻿using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;

//创建时间：2025-08-25
namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 打包任务
    /// </summary>
    [Verify]
    [Route("business/CksPackagingTask")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksPackagingTaskController : BaseController
    {
        /// <summary>
        /// 打包任务接口
        /// </summary>
        private readonly ICksPackagingTaskService _CksPackagingTaskService;

        public CksPackagingTaskController(ICksPackagingTaskService CksPackagingTaskService)
        {
            _CksPackagingTaskService = CksPackagingTaskService;
        }

        /// <summary>
        /// 查询打包任务列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "ckspackagingtask:list")]
        public IActionResult QueryCksPackagingTask([FromQuery] CksPackagingTaskQueryDto parm)
        {
            var response = _CksPackagingTaskService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询打包任务详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "ckspackagingtask:query")]
        public IActionResult GetCksPackagingTask(long Id)
        {
            var response = _CksPackagingTaskService.GetInfo(Id);

            var info = response.Adapt<CksPackagingTaskDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加打包任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "ckspackagingtask:add")]
        [Log(Title = "打包任务", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksPackagingTask([FromBody] CksPackagingTaskDto parm)
        {
            var modal = parm.Adapt<CksPackagingTask>().ToCreate(HttpContext);

            var response = _CksPackagingTaskService.AddCksPackagingTask(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新打包任务
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "ckspackagingtask:edit")]
        [Log(Title = "打包任务", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksPackagingTask([FromBody] CksPackagingTaskDto parm)
        {
            var modal = parm.Adapt<CksPackagingTask>().ToUpdate(HttpContext);
            var response = _CksPackagingTaskService.UpdateCksPackagingTask(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除打包任务
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "ckspackagingtask:delete")]
        [Log(Title = "打包任务", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksPackagingTask([FromRoute] string ids)
        {
            var idArr = Tools.SplitAndConvert<long>(ids);
            //删除前先解绑容器
            foreach (var item in idArr)
            {
                var one =InternalApp.Fsql.Select<CksPackagingTask>().Where(a => a.Id == item).First();
                var container = InternalApp.Fsql.Select<CksContainer>().Where(a => a.ContainerNo == one.ContainerNo).First();
                if(container.TaskNo == one.TaskNo && container.Status == 1)
                {
                    container.Status = 0;
                    InternalApp.Fsql.Update<CksContainer>().SetSource(container)
                        .UpdateColumns(a => a.Status).ExecuteAffrows();
                }
            }
            

            return ToResponse(_CksPackagingTaskService.Delete(idArr));
        }

    }
}